function buildMessage(message){
    var color = '';
    var side = '';
    if(message.to_teacher == "false"){
        color = 'panel-info';   
        side = 'pull-right';
    }
    else{
        color = 'panel-success';
        side = 'pull-left';
    }
    var content = '<div class="row">\
                <div class="col-xs-10 ' + side +'">\
                <div class="panel ' + color + '">\
                  <div class="panel-heading">\
                      <div class="row">\
                        <div class="col-lg-6 pull-right">\
                            <h3 class="panel-title">' + message.title + '</h3>\
                        </div>\
                        <div class="col-lg-6 text-left">\
                            ' + message.date + '\
                        </div>\
                      </div>\
                  </div>\
                  <div class="panel-body">\
                      <p>' + message.msg + '</p>\
                  </div>\
                    <div class="panel-footer text-left">\
                        <a onclick="createCustomModal(' + message.id + ')"><i class="fa fa-trash-o"></i></a>\
                  </div>\
                </div>\
              </div>\
            </div>';

    return content;
}

function showMessages(data){
    messageArr = JSON.parse(JSON.stringify(data));
    if (data.length == 0){
        console.log('no messages');
        var noMess = '<div class="alert alert-info">\
                    אין הודעות אישיות להציג עבור ילד זה</div>';
        $('#messages').html(noMess);
        return;
    }
    else{
        console.log('should display messages');   
        var messagesCode = '';
        for (var i = 0; i < messageArr.length; i++) {
            messagesCode += buildMessage(JSON.parse(messageArr[i]));
        }
        $('#messages').html(messagesCode);
    }
}


function getMessages(child_id){
    var temp = '<div class="alert alert-info">\
                    <i class="fa fa-spinner fa-spin"></i> ההודעות נטענות, נא להמתין</div>';
    $('#messages').html(temp);
    
    console.log(child_id);
    $.ajax('/childMessages', {
        type: 'GET',
        data: { 
            'fmt': 'json',
            'child_id': child_id
        },
        success: showMessages
    });
}

$(document).ready(function(){

})

